import random

import requests
from bs4 import BeautifulSoup

from api.login_register import LoginRegister
from config import URLPATH


class Recharge:
    # 获取充值验证码
    def get_recharge_verify(self, session, r):
        return session.get(url=f'{URLPATH}/common/public/verifycode/{r}')

    # 充值
    def recharge(self, session, amount, valicode, paymentType='chinapnrTrust', formStr='reForm'):
        return session.post(url=f'{URLPATH}/trust/trust/recharge', data={
            'paymentType': paymentType,
            'amount': amount,
            'formStr': formStr,
            'valicode': valicode
        }, headers={'Content-Type':'application/x-www-form-urlencoded'})

    # 三方充值
    def recharge_third(self, session, url, data):
        return session.post(url=url, data=data)

if __name__ == '__main__':
    re = Recharge()
    login = LoginRegister()
    session = requests.session()
    print('-' * 200)
    print(login.login_data(session, '18888888888', '123456').text)
    print('-' * 200)
    r = random.uniform(0,1)
    print(re.get_recharge_verify(session, r).text)
    print('-' * 200)
    print(re.recharge(session, '10000', '8888').json())
    print('-' * 200)

    # 获取第三方的充值信息,使用BeautifulSoup解析HTML
    html_data = re.recharge(session, '10000', '8888').json().get('description').get('form')
    print(html_data)
    soup = BeautifulSoup(html_data, 'html.parser')
    # 获取form标签中method属性的值
    print(soup.form['method'])
    # 获取form标签中action属性的值
    url = soup.form['action']
    print(soup.form['action'])
    # 获取input标签的所有name\value属性值,保证在字典中
    json_data = {}
    for input_tag in soup.find_all('input'):
        json_data[input_tag['name']] = input_tag['value']
    print(json_data)
    print(re.recharge_third(session, url, json_data).text)
